package com.tbit.uqbike.client.quartz;

import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.client.constant.GatewayProtocol;
import com.tbit.uqbike.client.constant.RabbitMQData;
import com.tbit.uqbike.client.constant.SystemConstant;
import com.tbit.uqbike.client.constant.SystemData;
import com.tbit.uqbike.client.pojo.MqData;
import com.tbit.uqbike.client.pojo.ReturnBikeMsg;
import com.tbit.uqbike.client.pojo.ReturnBikeMsgLog;
import com.tbit.uqbike.client.pojo.RideBattery;
import com.tbit.uqbike.client.pojo.newEntity.ReturnBikeLog;
import com.tbit.uqbike.client.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: chen
 * @Description: 定时推送服务器
 * @Date: 创建于 10:40 2020/11/27
 */
@Component
@Slf4j
public class PushMsgToMqQuartz {


    @Autowired
    private TerControlService terControlService;
    @Autowired
    private ReturnBikeMsgService returnBikeMsgService;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private ReturnBikeLogService returnBikeLogService;
    @Autowired
    private ReturnBikeMsgLogService returnBikeMsgLogService;

    @Scheduled(cron = "0 0/10 * * * ?")
    public void push() {
        log.info("开始定时推送任务push");
        try {
            List<MqData> list = SystemData.pushList;
            Map<String, RideBattery> map = SystemData.pushMap;
            /**将缓存置空*/
            SystemData.pushList = new ArrayList<>();
            SystemData.pushMap = new ConcurrentHashMap<>();
            /**推送开关打开*/
            if (SystemConstant.PUSH_BUTTON.equals("1")) {
                /**迭代获取值*/
                Set<Entry<String, RideBattery>> entrySet = map.entrySet();
                if (!entrySet.isEmpty()) {
                    for (Entry<String, RideBattery> set : entrySet) {
                        MqData mqData = new MqData();
                        mqData.setFeedback(RabbitMQData.aiQueue);
                        mqData.setMsgId(GatewayProtocol.MQ_MSG_ID_AROUND_RIDE_MSG_PUSH);
                        mqData.setData(set.getValue());
                        list.add(mqData);
                    }
                }

                if (list.size() > 0) {
                    log.info("定时推送任务" + list.size());
                    terControlService.sendToQueue(JSON.toJSONString(list));
                }
            }
            map.clear();
        }catch (Exception e){
            log.error("定时任务push执行异常",e);
        }

    }

    @Scheduled(cron = "0 0/10 * * * ?")
    public void bikeMsgHandler() {
        log.info("开始定时推送任务bikeMsgHandler");
        try {
            List<ReturnBikeMsg> list = SystemData.bikeMsgList;
            /**将缓存置空*/
            SystemData.bikeMsgList = new ArrayList<>();
            /**推送开关打开*/
            if (SystemConstant.PUSH_BUTTON.equals("1")) {
                if (list.size() > 0) {
                    for (ReturnBikeMsg returnBikeMsg : list) {
                        returnBikeMsgService.add(returnBikeMsg);
                    }
                }
            }
            list.clear();
        }catch (Exception e){
            log.error("定时任务bikeMsgHandler执行异常",e);
        }

    }

  /*@Scheduled(cron = "0 0/5 * * * ?")
  public void ridelogHandler(){
    Map<String,Map<String,Object>> rideMap = SystemData.ridelogMap;
    SystemData.ridelogMap=new ConcurrentHashMap<>();
    if(!rideMap.isEmpty()){
        rideMap.forEach((key, value) -> {
          AliLogUtil.sendLog(value,"tbit-client-test","cn-hangzhou.log.aliyuncs.com","LTAIdfKErZxxWF5u","OScVLrYDs6A4ys76IatLrWBqxmHUVt");
        });
    }
    log.info("推送日志"+rideMap.size());
    rideMap.clear();
  }*/


    @Scheduled(cron = "0 0/10 * * * ?")
    public void logHandler() {
        log.info("开始定时推送任务logHandler");
        try {
            List<ReturnBikeLog> list = SystemData.logList;
            /**将缓存置空*/
            SystemData.logList = new ArrayList<>();
            /**推送*/
            if (list.size() > 0) {
                returnBikeLogService.addBatch(list);
            }

            list.clear();
        }catch (Exception e){
            log.error("定时任务logHandler执行异常",e);
        }

    }

    @Scheduled(cron = "0/5 * * * * ?")
    public void returnBikeMsgLogHandler() {
        log.info("开始定时推送任务returnBikeMsgLogHandler");
        try {
            List<ReturnBikeMsgLog> list = SystemData.returnBikeMsgLogList;
            SystemData.returnBikeMsgLogList = new ArrayList<>();
            if (list.size() > 0) {
                returnBikeMsgLogService.addBatch(list);
            }
            list.clear();
        }catch (Exception e){
            log.error("定时任务returnBikeMsgLogHandler执行异常",e);
        }
    }

}
